package com.china08.yxyapi.api.manage.sch.platform.statistics.resource;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.res.YxyResResource;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyResResourceRepositoryTwo;

import io.swagger.annotations.ApiOperation;

/**
 * 教育局平台办公 资源统计学校 资源统计 学段接口
 * 
 */
@CrossOrigin
@RestController
public class ResourcePhaseNameController extends YxyApiBaseController{
	@Autowired
	private YxyResResourceRepositoryTwo yxyResResourceRepository;
	
	@ApiOperation(value = "资源统计学校 资源统计 学段接口")
	@ResponseBody
	@RequestMapping(value = "/api_yw/manage/sch/platform/statistics/resource/phasename/srh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public RespModel precess(HttpServletRequest request, HttpServletResponse response) {
		String schoolId = getProfile(request, response).getSchoolId();
		Set<String> phaseNameList1=new HashSet<String>();
		List<YxyResResource> yxyResResourceLst=yxyResResourceRepository.findByUploaderSchoolId(schoolId);
		for (YxyResResource yxyResResource : yxyResResourceLst) {
			phaseNameList1.add(yxyResResource.getPhaseName());
		}
		List<String> phaseLst =new ArrayList<>();
		String[] phaseNameList=new String[]{"幼儿园", "小学", "初中","高中", "大学"};
		
		for (String subjectName : phaseNameList) {
			for (String phaseName : phaseNameList1) {
				if (subjectName.equals(phaseName)) {
					phaseLst.add(phaseName);
				}
				
			}
		}
		String phase="";
		for (String name : phaseLst) {
			phase+=name+",";
		}
		//判断排序后的学段有没有在原来的基础上遗漏，如果有就及时补救
		if (phaseLst.size()<phaseNameList1.size()) {
			for (String string : phaseNameList1) {
				if (phase.indexOf(string)==-1) {
					phaseLst.add(string);
				}
			}
		}
		RespModel model=new RespModel();
		model.phaseLst=phaseLst;
		return model;
	}

	protected static class RespModel extends YxyApiRespModel {
		//学段集合
		public List<String> phaseLst;
	}
}
